using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using SimpleServiceBus.Bus.MessageManagement.Serialization;
using System.Xml.Serialization;
using SimpleServiceBus.Endpoint;
using SimpleServiceBus.Bus.MessageManagement;
using System.Xml;

namespace SimpleServiceBus.Endpoints.ActiveMQ.Transport {

    public class ActiveMQXmlMessageSerializer: XmlMessageSerializer, IXmlMessageSerializer {

        //-----< Fields >-----
        //----------------------------------------------------------------------------------        
        private XmlSerializer _xmlSerializer;


        //-----< Constructors >-----
        //----------------------------------------------------------------------------------        

        public ActiveMQXmlMessageSerializer() {
        }

        //-----< Public and protected properties >-----
        //----------------------------------------------------------------------------------        

        //-----< Public and protected methods >-----
        //----------------------------------------------------------------------------------        

        public Object Deserialize(XmlReader reader) {
            if(!IsRunning)
                throw new InvalidOperationException(
                    "The XmlMessageSerializer is not running. Please call Start prior to using this service.");
            return _xmlSerializer.Deserialize(reader);
        }

        public void Serialize(object message, System.Xml.XmlWriter writer) {
            if(!IsRunning)
                throw new InvalidOperationException(
                    "The XmlMessageSerializer is not running. Please call Start prior to using this service.");
            _xmlSerializer.Serialize(writer, message);
        }

        protected override void ShutDownService() {
            base.ShutDownService();
            _xmlSerializer = null;
        }

        protected override void StartService() {
            base.StartService();
            Initialize();
        }

        //-----< Events >-----
        //----------------------------------------------------------------------------------        


        //-----< Internal members >-----
        //----------------------------------------------------------------------------------        


        //-----< Private members >-----
        //----------------------------------------------------------------------------------        

        private void Initialize() {
            var _knownTypes = KnownTypes;
            if(_knownTypes == null)
                _knownTypes = new Type[0];
            var messageTypes = new List<Type>(_knownTypes);
            var msgMgr = ServiceManager.GetService<IMessageTypeManagementService>();
            if(msgMgr != null) {
                messageTypes.AddRange(msgMgr.GetRegisteredMessageTypes());
            }
            _xmlSerializer = new XmlSerializer(typeof(object), messageTypes.ExtractSerializableTypes());
        }

        //-----< All nested types >-----
        //----------------------------------------------------------------------------------        


        //-----< All explicit interface implementations >-----
        //----------------------------------------------------------------------------------        



    }
}

